﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.BLL;
using Baza.EF.DAL;

namespace Pep_3014_3070.Anonymous
{
    public partial class Home : System.Web.UI.Page
    {
        public Reservations rezervacija
        {
            get
            {
                if (Session["rezervacija"] == null)
                {
                    Session["rezervacija"] = new Reservations();
                }
                return (Reservations)Session["rezervacija"];
            }
            set
            {
                Session["rezercavija"] = value;
            }
        }
        protected int totalNumberPost
        {
            get { return (int)ViewState["totalPostC"]; }
            set { ViewState["totalPostC"] = value; }
        }
        protected int startPage
        {
            get { return (int)ViewState["StartPageC"]; }
            set { ViewState["StartPageC"] = value; }
        }
        protected int maxPage
        {
            get { return (int)ViewState["MaxPageC"]; }
            set { ViewState["MaxPageC"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                totalNumberPost = DACustomers.getCountApartman();
                startPage = 0;
                maxPage = 8;
                BindData();

            }
        }


        protected string Datum;
        private void BindData()
        {
            DateTime dt = DateTime.Now;

            Datum = string.Format("{0:m}", dt);

            List<Cities> temp = DACustomers.GetAllCities();
            Cities cit = new Cities();
            cit.Name = "Odaberi Grad";
            cit.CityID = 0;
            temp.Insert(0, cit);

            CityInputDropDownList.DataSource = temp;
            CityInputDropDownList.DataTextField = "Name";
            CityInputDropDownList.DataValueField = "CityID";
            CityInputDropDownList.DataBind();

            //Ovdje editovati apartmane koji su na pocetku ucitani !!!
            BindTopApartman();

        }

        //Ucitavanje apartmana na pocetku , za anonimne korisnike [ BEZ BILO KAKVIH USLOVA PRETRAGE  ] | po defaultu ucitavaju se apartmani sa cijenom za 7 nocenja!
        private void BindTopApartman()
        {
            DateTime dt = DateTime.Now;

            Datum = string.Format("{0:m}", dt);
            List<ad_Customers_GetApartmans_Search_Result> tempApartm = DACustomers.getApartman(startPage, maxPage);

            int razlika = 7;

            for (int i = 0; i < tempApartm.Count; i++)
            {
                tempApartm[i].Price = Convert.ToDecimal(0);
                for (int j = 0; j < razlika; j++)
                {
                    Prices tempPrice = new Prices();
                    tempPrice = DACustomers.getPriceByApartmanID(Convert.ToInt32(tempApartm[i].ApartmentID), Convert.ToDateTime(System.DateTime.Now),
                                                                                                            Convert.ToDateTime(System.DateTime.Now).AddDays(j + 1));
                    if (tempPrice != null)
                        tempApartm[i].Price += Convert.ToDecimal(tempPrice.Price);
                }
                if (Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                    tempApartm[i].Price *= Convert.ToInt32(GuestDropDownList.SelectedIndex);

            }

            ApartmaniRepeater.DataSource = tempApartm;
            ApartmaniRepeater.DataBind();
            ApartmaniUpdatePanel.Focus();
            ApartmaniRepeater.Focus();


            rezervacija.CheckIn = DateTime.Now;
            rezervacija.CheckOut = DateTime.Now.AddDays(7);
            rezervacija.Price = 0;
            if (Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                rezervacija.NumberGuest = Convert.ToInt32(GuestDropDownList.SelectedIndex);
            else
                rezervacija.NumberGuest = 1;

        }

        //Ucitavanje apartmana sa odredjenim uslovima pretrage, takodjer za anonimne korisnike
        private void BindApartman()
        {

            DateTime cIn;
            DateTime cOut;
                    
            if (CheckInInput.Text == "")
                cIn = DateTime.Now;
            else
                cIn = Convert.ToDateTime(CheckInInput.Text);

            if (CheckOutInput.Text == "")
                cOut = DateTime.Now;
            else
                cOut = Convert.ToDateTime(CheckOutInput.Text);

            if (cOut > cIn && cIn >= DateTime.Now)
            {
                DateTime dt = DateTime.Now;

                Datum = string.Format("{0:m}", dt);

                List<ad_Customers_Apartman_SELECT_BY_CitYID_Result> tempApartm =
                    DACustomers.getApartmansByCityID(Convert.ToInt32(CityInputDropDownList.SelectedIndex), startPage, maxPage,
                    Convert.ToDateTime(CheckInInput.Text), Convert.ToDateTime(CheckOutInput.Text));

                int razlika = (Convert.ToDateTime(CheckOutInput.Text) - Convert.ToDateTime(CheckInInput.Text)).Days;

                for (int i = 0; i < tempApartm.Count; i++)
                {
                    tempApartm[i].Price = Convert.ToDecimal(0);
                    for (int j = 0; j < razlika; j++)
                    {
                        Prices tempPrice = new Prices();
                        tempPrice = DACustomers.getPriceByApartmanID(Convert.ToInt32(tempApartm[i].ApartmentID), Convert.ToDateTime(CheckInInput.Text), Convert.ToDateTime(CheckInInput.Text).AddDays(j + 1));
                        if (tempPrice != null)
                            tempApartm[i].Price += Convert.ToDecimal(tempPrice.Price);
                    }
                    if (Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                        tempApartm[i].Price *= Convert.ToInt32(GuestDropDownList.SelectedIndex);

                }

                ApartmaniRepeater.DataSource = tempApartm;
                ApartmaniRepeater.DataBind();
                ApartmaniUpdatePanel.Focus();
                ApartmaniRepeater.Focus();

                rezervacija.CheckIn = Convert.ToDateTime(CheckInInput.Text);
                rezervacija.CheckOut = Convert.ToDateTime(CheckOutInput.Text);

                rezervacija.Price = 0;

                if (Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                    rezervacija.NumberGuest = Convert.ToInt32(GuestDropDownList.SelectedIndex);
                else
                    rezervacija.NumberGuest = 1;


            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "$(document).ready(function(){alert('Unesite ispravno datum dolaska i datum odlaska !')});", true);
        }

        protected void SrchButton_Click(object sender, EventArgs e)
        {
            BindApartman();

        }

        protected void NextLinkButton_Click(object sender, EventArgs e)
        {
            if (startPage + 8 >= 0 && maxPage <= totalNumberPost)
            {
                startPage += 8;
                maxPage += 8;

                if (CheckInInput.Text != "" && CheckOutInput.Text != "" && Convert.ToInt32(CityInputDropDownList.SelectedIndex) != 0 && Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                    BindApartman();
                else
                    BindTopApartman();
            }
        }

        protected void PreviousLinkButton_Click(object sender, EventArgs e)
        {
            if (startPage - 8 >= 0 && maxPage - 8 >= 0)
            {
                startPage -= 8;
                maxPage -= 8;

                if (CheckInInput.Text != "" || CheckOutInput.Text != "" || Convert.ToInt32(CityInputDropDownList.SelectedIndex) != 0 || Convert.ToInt32(GuestDropDownList.SelectedIndex) != 0)
                    BindApartman();
                else
                    BindTopApartman();

            }
        }


    }
}